const app = getApp()
const api = app.globalData.api
Page({
    data: {
      project: app.project,
      code: '',
      userName: '',
      password: '',
    },
    onLoad: async function (options) {
      if(options.code){
        let root = app.domainName + (app.project ? options.code + '/' : '')
        const resp = await api.curlPost('?cla=loginApp&fun=loginPWD', {
          rk: options.rk,
          token: options.token,
          source: 'wechat',
        }, root + 'interface/v1/')
        if (resp.code === 1) {
          this.successCallBack(resp.data.token, root, root + 'interface/v1/')
        }
      }else{
        if(!options.logout){
          // 尝试微信登录
          this.wechatLogin()
        }
        this.setData({
          code: wx.getStorageSync('code'),
          userName: wx.getStorageSync('userName'),
          password: wx.getStorageSync('password'),
        })
      }
    },
    onShow: function (options) {
      wx.hideHomeButton();
    },
    successCallBack(token, realDomain, root) {
      wx.setStorageSync('token', token)
      wx.setStorageSync('realDomain', realDomain)
      wx.setStorageSync('realUrl', root)
      app.resetGlobalData(realDomain, root);
      // 重新启动小程序
      let fromUrl = wx.getStorageSync('fromUrl')
      let url = decodeURIComponent(fromUrl)
      if(fromUrl && url.indexOf('/pages/login/index')  === -1){
        if(url.indexOf('/pages/index/index')  !== -1){
          wx.switchTab({
            url: url
          })
        }else{
          wx.redirectTo({
            url: url
          })
        }
      }else{
        wx.switchTab({
          url: '/pages/index/index'
        })
      }
    },
    commit: async function (e) {
      const {code, userName, password} = e.detail.value
      let root = app.domainName + (app.project ? code + '/' : '')
      if (app.project && code.length < 1) {
        api.showToast('请输入企业代码')
      } else if(userName.length < 1) {
        api.showToast('请填写用户名！')
      } else if(password.length < 1){
        api.showToast('请填写密码！')
      } else {
        const resp = await api.curlPost('?cla=loginApp&fun=loginPWD', {
          userName: userName,
          password: password,
          source: 'wechat',
          openid: wx.getStorageSync('openid'),
        }, root + 'interface/v1/')
        if (resp.code === 1) {
          wx.setStorageSync('code', code)
          wx.setStorageSync('userName', userName)
          wx.setStorageSync('password', password)
          this.successCallBack(resp.data.token, root, root + 'interface/v1/')
        }
      }
    },
    /**微信小程序登录 */
  wechatLogin() {
    wx.showLoading({
      title: '自动登录中。。。',
      mask: true
    })
    let that = this;
    wx.removeStorageSync('token')
    wx.login({
        success: async function (res) {
            if (res.code) {
                //发起网络请求
                const resp = await api.curlPost('?cla=loginApp&fun=weChatLogin', {
                    code: res.code,
                    platform: 'wechat',
                }, that.root)
                if (resp.code === 1) {
                  wx.setStorageSync('openid', resp.data.openid)
                  if(resp.data.token){
                    that.successCallBack(resp.data.token, app.domain, app.root)
                  }
                }else{
                  api.showToast('首次登录需要绑定账号')
                }
            } else {
                api.showToast('登录失败！' + res.errMsg)
            }
        }
    });
  },
});